<?php
namespace Gotcha\Core\Components\Debugging {
    use Gotcha\Core\Components\Debugging\Interfaces\ILogger;

    class Logger implements ILogger {
        const LOG_FOLDER = LOGS_DIR;
        const LOG_FILE_NAME = 'Gotcha-Log-';

        static function Log($dataToLog, $traceToLog = null)
        {
            // Date to be used with current log
            $currentDate = new \DateTime('now', new \DateTimeZone(SYSTEM_DEFAULT_TIMEZONE));

            // Data to be saved
            if(!is_null($traceToLog)) {
                $traceParts = new \ArrayObject(explode('#', $traceToLog));
                $traceToLog = implode(PHP_EOL, $traceParts->getArrayCopy()) . PHP_EOL . PHP_EOL;
            }
            $combinedData = '[' . $currentDate->format('Y-m-d H:i:s') . '] ' . $dataToLog . PHP_EOL . $traceToLog;

            // Full log file name containing system prefix + creation date
            $logFileFullName = self::LOG_FILE_NAME . $currentDate->format('Y-m-d');
            file_put_contents(self::LOG_FOLDER . $logFileFullName . '.txt', $combinedData, FILE_APPEND | LOCK_EX);
        }
    }
}